<template>
  <el-container class="content-view">
    <el-header>
      <div>电商后台管理系统</div>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 导航菜单  -->
      <el-aside width="200px">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          router
          :default-active='activepath'
        >
          <el-submenu :index="'/'+item.path" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item @click="saveActive('/'+items.path)" :index="'/'+items.path" v-for="items in item.children" :key="items.id">{{items.authName}}</el-menu-item>
            
          </el-submenu>


        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList:[],
      activepath:''
    };
  },
  created() {
    this.getmenu()
    this.activepath = sessionStorage.getItem('activePath')
  },
  methods: {
    //点击退出登录
    exit() {
      var flag = confirm("确定要退出吗");

      if (flag) {
        sessionStorage.clear();
        this.$router.push("/login");
      }
    },
    
    //获取管理菜单
    async getmenu(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status !== 200){
         return this.$message.error('渲染管理列表失败')
      }

      this.menusList = res.data
      
    },
    //保存菜单被选中状态
    saveActive(path){
      sessionStorage.setItem('activePath',path)
      this.activepath = path
    }
  }
};
</script>

<style scoped>
.content-view {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
}

.el-aside {
  background-color: #333744;
}

.el-menu{
  border-right:none;
}
</style>
